package org.study.tk.boot.service;

import com.github.pagehelper.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.study.tk.boot.mapper.StudentMapper;
import org.study.tk.boot.model.Student;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Random;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-04-20
 */
@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insert(Student student) {
        int insert = studentMapper.insert(student);
        int random = new Random().nextInt() % 2;
        log.info("当前random：{}", random);
        if (random % 2 == 0) {
            throw new RuntimeException("异常");
        }
        return insert;
    }

    @Override
    public int insertSelective(Student student) {
        return studentMapper.insertSelective(student);
    }

    @Override
    public int delete(Student student) {
        return studentMapper.delete(student);
    }

    @Override
    public int deleteByPrimaryKey(Student student) {
        return studentMapper.deleteByPrimaryKey(student);
    }

    @Override
    public int updateByPrimaryKeySelective(Student student) {
        return studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    public Student selectByPrimaryKey(Student student) {
        return studentMapper.selectByPrimaryKey(student);
    }

    @Override
    public Student selectOne(Student student) {
        return studentMapper.selectOne(student);
    }

    @Override
    public List<Student> selectByExample(Example example) {
        return studentMapper.selectByExample(example);
    }

    @Override
    public List<Student> selectAll() {
        return studentMapper.selectAll();
    }

    @Override
    public PageInfo<Student> startPage1(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> selectAll = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (!CollectionUtils.isEmpty(selectAll)) {
            pageInfo = new PageInfo<>(selectAll);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> startPage2(int pageNum, int pageSize) {
        Page<Student> studentPage = PageHelper.startPage(pageNum, pageSize);
        List<Student> selectAll = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (!CollectionUtils.isEmpty(selectAll)) {
            pageInfo = new PageInfo<>(studentPage);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> offsetPage1(int pageNum, int pageSize) {
        PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<Student> selectAll = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (!CollectionUtils.isEmpty(selectAll)) {
            pageInfo = new PageInfo<>(selectAll);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> offsetPage2(int pageNum, int pageSize) {
        Page<Student> studentPage = PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize);
        List<Student> selectAll = studentMapper.selectAll();
        PageInfo<Student> pageInfo = new PageInfo<>();
        if (!CollectionUtils.isEmpty(selectAll)) {
            pageInfo = new PageInfo<>(studentPage);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Student> doSelectPageInfoByStartPage(int pageNum, int pageSize) {
        PageInfo<Student> pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> studentMapper.selectAll());
        return pageInfo;
    }

    @Override
    public PageInfo<Student> doSelectPageInfoByOffsetPage(int pageNum, int pageSize) {
        PageInfo<Student> pageInfo = PageHelper.offsetPage((pageNum - 1) * pageSize, pageSize)
                .doSelectPageInfo(() -> studentMapper.selectAll());
        return pageInfo;
    }

    @Override
    public long count() {
        long count = PageHelper.count(() -> studentMapper.selectAll());
        return count;
    }

    @Override
    public List<Student> myPageSelect1(Integer pageNum, Integer pageSize) {
        return studentMapper.myPageSelect1(pageNum, pageSize);
    }

    @Override
    public List<Student> myPageSelect2(Integer pageNum, Integer pageSize) {
        return studentMapper.myPageSelect2(pageNum, pageSize);
    }
}
